package com.stwitinc.redmine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stwitinc.Logger;
import com.stwitinc.redmine.dao.DAO;

public class LuceneUtil
{
	private static final Logger logger = Logger.getLogger(LuceneUtil.class);
	private static File dir;

	public static List<Integer> search(String keyword) throws Exception {
		Directory directory = FSDirectory.open(dir);
		IndexReader reader = IndexReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
		QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_34, new String[] { "genericName", "tradeName" },
				analyzer);
		Query query = parser.parse(keyword);
		TopDocs topDocs = searcher.search(query, 100);
		logger.debug("totalHits=" + topDocs.totalHits);
		List<Integer> medicineIdList = new ArrayList<Integer>();
		for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
			Document doc = searcher.doc(scoreDoc.doc);
			int medicineId = Integer.parseInt(doc.get("medicineId"));
			medicineIdList.add(medicineId);
		}
		searcher.close();
		return medicineIdList;
	}

	public static void buildIndex(File dir) throws Exception {/*
		LuceneUtil.dir = dir;
		logger.info("读取药品说明书信息...");
		JSONArray array = DAO.newMedicineDAO().getPackageInsertSummary();
		logger.info("建立药品说明书索引..." + array.size());
		// Analyzer analyzer = new IKAnalyzer(true);
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_34);
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_34, analyzer);
		cfg.setOpenMode(OpenMode.CREATE);
		Directory directory = FSDirectory.open(dir);
		IndexWriter w = new IndexWriter(directory, cfg);
		for (int i = 0; i < array.size(); i++) {
			JSONObject obj = array.getJSONObject(i);
			String medicineId = obj.getString("medicineId");
			String genericName = obj.getString("genericName");
			String tradeName = obj.getString("tradeName");
			Document doc = new Document();
			if (genericName != null) {
				doc.add(new Field("genericName", genericName, Field.Store.NO, Field.Index.ANALYZED));
			}
			if (tradeName != null) {
				doc.add(new Field("tradeName", tradeName, Field.Store.NO, Field.Index.ANALYZED));
			}
			doc.add(new Field("medicineId", medicineId, Field.Store.YES, Field.Index.NOT_ANALYZED));
			w.addDocument(doc);
		}
		w.commit();
		w.close();
	*/}
}
